package cn.wit.front.service.impl;

import cn.wit.front.mapper.*;
import cn.wit.front.service.UserService;
import cn.wit.front.utils.AesUtils;
import cn.wit.front.vo.UserVO;
import cn.wit.utils.pojo.*;
import cn.wit.utils.response.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huaban.analysis.jieba.JiebaSegmenter;
import com.huaban.analysis.jieba.SegToken;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private BaoyanMapper baoyanMapper;
    @Autowired
    private JobCategoryMapper jobCategoryMapper;
    @Autowired
    private ContestMapper contestMapper;

    @Override
    public Result updateUser(User user) {
        int i = userMapper.updateById(user);
        if (i > 0) {
            return Result.ok("更新成功");
        }
        return Result.error("更新失败");
    }

    @Override
    public Result selectByUserName(String username) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        User user = userMapper.selectOne(wrapper);
        Integer jobCategoryId = user.getJobCategoryId();
        Integer contestId = user.getContestId();
        Integer baoyanId = user.getBaoyanId();
        Baoyan baoyan = baoyanMapper.selectById(baoyanId);
        Contest contest = contestMapper.selectById(contestId);
        JobCategory jobCategory = jobCategoryMapper.selectById(jobCategoryId);

        UserVO vo = new UserVO();
        if (baoyan != null) {
            vo.setBaoyan(baoyan);
        }
        if (contest != null) {
            vo.setContest(contest);
        }
        if (jobCategory != null) {
            vo.setJobCategory(jobCategory);
        }
        vo.setName(user.getName());
        vo.setId(user.getId());
        vo.setUsername(user.getUsername());
        vo.setStudentClass(user.getStudentClass());
        return Result.ok(user);
    }

    @Override
    public Result updatePwd(String username, String oldPassword, String newPassword) throws Exception {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        User user = userMapper.selectOne(wrapper);
        if (user == null) {
            return Result.error("修改密码失败");
        }
        String decrypt = null;
        try {
            decrypt = AesUtils.decrypt(user.getPassword(), user.getPwKey());
        } catch (Exception e) {
            return Result.error("修改密码失败");
        }
        if (oldPassword.equals(decrypt)) {
            User build = User.builder()
                    .id(user.getId())
                    .password(AesUtils.encrypt(newPassword, user.getPwKey()))
                    .build();
            int i = userMapper.updateById(build);
            if (i > 0) {
                return Result.ok();
            }
            return Result.error("修改密码失败");
        }
        return Result.error("修改密码失败");
    }










}
